// @author U099151W Sun Chenchen
// all functions in this file  are implemented by the author unless specifically indicated
var Building = function() {
    Grid.call(this);
    this.isEmpty = function() {
        var NPCs = this.getNPCs();
        for (var type in NPCs) {
            if (NPCs[type].length !== 0) {
                return false;
            }
        }
        return true;
    };
    var _portalList  = [
        {x : 0,
         y : 0}
    ];
    this.setPortals = function(portals) {
        _portalList = portals;
    };
    var _outsidePortals = [];
    this.setOutsidePortals = function(portal) {
        _outsidePortals =  portal
    };
    this.getOutsidePortals = function() {
        return _outsidePortals;
    };

    this.removeAllNPCs = function() {
        var NPCs = this.getNPCs();
        for (var type in NPCs) {
            delete NPCs[type];
            NPCs[type] = [];
        }
    };

    var _grid;
    this.setGrid = function(grid) {
        // not safe!!!
        _grid = grid;
    };
    this.getOpposite = function() {
        return {
            grid : _grid,
            portals : _outsidePortals
        }
    };
    this.getPortals = function() {
        return _portalList;
    };

    this.drawBackground = function(canvas, sx, sy, w, h) {
        var img = document.getElementById(this.getType());
        var context = canvas.getContext("2d");
        context.drawImage(img, sx, sy, w, h);
    };

    this.getObstacleMap = function() {
        var originalMap = ObstacleMap[this.getType()];
        var localMap = [];
        for (var i=0; i<originalMap.length; i++) {
            var newRow = [];
            for (var j=0; j<originalMap[0].length; j++) {
                newRow.push(originalMap[i][j]);
            }
            localMap.push(newRow);
        }
        // obstacles
        var _NPCs = this.getNPCs();
        for (var oneTypeOfNPC in _NPCs) {
            for (var i= 0; i < _NPCs[oneTypeOfNPC].length; i++) {
                var oneNPC =  _NPCs[oneTypeOfNPC][i];
                localMap[oneNPC.getPosition().y][oneNPC.getPosition().x] = 1;
            }
        }
        return localMap;
    };

};

Building.prototype = new Grid();
Building.prototype.constructor = Building;